<template>
  <div>
    ==={{ checkList }}
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin: 40px 0; text-align: center">
      <el-button>取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { getRoles } from '@/api/setting.js'
import { assignRoles } from '@/api/employees.js'
export default {
  name: '',
  props: ['roleIds', 'userId'],
  data() {
    return {
      roleParams: {
        page: 1,
        pagesize: 100
      },
      roleList: [],
      checkList: []
    }
  },
  watch: {
    roleIds: {
      handler(newVal) {
        //  alert(12345)
        this.checkList = newVal
      }
    }
  },
  created() {
    this.getRolesList()
    //  获取当前用户所拥有的角色

    //  this.checkList = this.roleIds
  },
  methods: {
    async getRolesList() {
      const res = await getRoles(this.roleParams)
      this.roleList = res.data.rows
    },
    async submit() {
      /*
      步骤分析：
      1-点击确认按钮
      2-封装api接口函数
      3-调用函数并传入参数
        ===需要在父组件中传递用户id给当前分配角色对话框组件
      4-成功：关闭对话框
      */
      const res = await assignRoles({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$emit('success')
 
    }
  }
}
</script>
<style lang="less" scoped></style>
